package com.example.administrator.videotest2;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.example.administrator.videotest2.entity.DS360_dt;
import com.example.administrator.videotest2.util.HttpUtil;
import com.example.administrator.videotest2.util.JsonUtil;
import com.umeng.analytics.MobclickAgent;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class Vdetail_Activity extends AppCompatActivity{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //隐去标题栏（应用程序的名字）

        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_vdetail_);
        String str=getIntent().getStringExtra("str");;
        String image=getIntent().getStringExtra("image");
        queryFromServer(str+image);
    }

    private void queryFromServer(String address){

        HttpUtil.sendOKHttpRequest(address, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

               Vdetail_Activity.this.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(Vdetail_Activity.this.getBaseContext(),"加载失败",Toast.LENGTH_SHORT);
                    }
                });
            }

            @Override
            public void onResponse(Call call, final Response response) throws IOException {

                final String responseText=response.body().string();
                final DS360_dt result= JsonUtil.handle_video(responseText);
                if(result!=null) {
                    Vdetail_Activity.this.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {

                            if (result.getDirector() != null) {
                                TextView director = (TextView) findViewById(R.id.director);
                                director.setText("导演："+result.getDirector());
                            }
                            if (result.getActor() != null) {
                                TextView actor = (TextView) findViewById(R.id.actor);
                                actor.setText("主演："+result.getActor());
                            }

                            if (result.getYear() != null) {
                                TextView year = (TextView) findViewById(R.id.year);
                                year.setText("年份："+result.getYear());
                            }
                            if (result.getArea() != null) {
                                TextView area = (TextView) findViewById(R.id.area);
                                area.setText("地区："+result.getArea());
                            }
                            if (result.getType() != null) {
                                TextView type = (TextView) findViewById(R.id.type);
                                type.setText("类型："+result.getType());
                            }
                            if (result.getScore() != null) {
                                TextView score = (TextView) findViewById(R.id.score);
                                score.setText("评分："+result.getScore());
                            }
                            if (result.getDetail() != null) {
                                TextView detail = (TextView) findViewById(R.id.detail);
                                detail.setText("详情："+result.getDetail());
                            }
                            if (result.getImage() != null) {
                                ImageView img= (ImageView) findViewById(R.id.img);
                                Glide.with(Vdetail_Activity.this).load(result.getImage()).centerCrop().into(img);

                            }
                            if (result.getTitle()!= null) {
                                TextView title = (TextView) findViewById(R.id.title_name);
                                title.setText(result.getTitle());
                            }

                            ImageView player= (ImageView) findViewById(R.id.player);

                            player.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    Intent player_intent=new Intent(Vdetail_Activity.this,ToPlayer_Activity.class);
                                    player_intent.putExtra("url",result.getPurl());
                                    startActivity(player_intent);
                                }
                            });

                        }
                    });
                }
            }
        });
    }
    public void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }
    @Override
    public void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }

}
